<?php
namespace backend\controllers;

use backend\models\AdminRoleModel;
use backend\models\AppVersionModel;
use backend\models\ConfigModel;
use backend\models\OssModel;
use Yii;
use backend\base\BaseController;

/**
 * Config controller
 */
class ConfigController extends BaseController
{
    /*
     * 列表
     */
    public function actionList()
    {
        $refresh = $this->post('refresh',0);
        $class = $this->get('class',1);
        //刷新缓存
        if($refresh){
            return $this->refreshCache();
        }
        if($class == 'app_version'){
            return $this->app_version_list();
        }
        if($class == 0 && Yii::$app->user->id != 1){
            return $this->redirect(['site/auth-error']);
        }
        $list = ConfigModel::find()->where(['class'=>$class])->orderBy('status desc,id asc')->all();
        //获取上传参数
        $ossUpload = OssModel::getOssUpload();
        return $this->render('list',['list' => $list,'class'=>$class,'ossUpload'=>$ossUpload]);
    }

    /**
     * 编辑
     */
    public function actionEditAjaxDo()
    {
        $data = $this->post(null);
        if(isset($data['type']) && $data['type'] == 'app_version'){
            return $this->app_version_edit($data);
        }
        if(!isset($data['class']) || ($data['class'] == 0 && Yii::$app->user->id != 1)){
            return json_encode(parent::ajaxCustom('80000','参数错误或没有权限'));
        }
        $model = ConfigModel::findOne($data['id']);
        if($model){
            $model->setScenario('edit');
        }else{
            $model = new ConfigModel(['scenario'=>'add']);
        }
        if ($model->load($data,'') && $model->validate() && $model->save()) {
            $this->addRecord('编辑配置',$model->id);
            return json_encode(parent::ajaxReturn('operate_success'));
        }
        return json_encode(parent::ajaxReturn('edit_error'));
    }

    /*
     * 修改状态,禁用|解禁
     */
    public function actionStatusAjaxDo()
    {
        $data = parent::parseData($this->body);
        $model = ConfigModel::findOne($data['id']);
        if ($model) {
            $model->status = intval($data['status']);
            if($model->save()){
                $this->addRecord('禁用|解禁配置',$data['id']);
                return json_encode(parent::ajaxReturn('operate_success', []));
            }
        }
        return json_encode(parent::ajaxReturn('edit_error', []));
    }

    /**
     * 刷新缓存
     * @param $class
     * @return string
     * @throws \yii\base\Exception
     */
    private function refreshCache(){
        $query = ConfigModel::find();
        if(Yii::$app->user->id != 1) {
            $query->andWhere(['<>','class',1]);
        }
        $list = $query->all();
        if($list){
            foreach($list as $key => $val){
                if($val->status == 1){
                    ConfigModel::cacheSet($val->key,$val->value);
                }else{
                    ConfigModel::cacheDel($val->key);
                }
            }
            $this->addRecord('刷新配置',0);
        }
        return json_encode(parent::ajaxReturn('operate_success'));
    }

    private function app_version_list(){
        $list = AppVersionModel::find()->all();
        //获取上传参数
        $ossUpload = OssModel::getOssUpload('apk','kds_');
        return $this->render('app-version-list',['list' => $list,'ossUpload'=>$ossUpload,'class'=>'app_version','class_arr'=>AppVersionModel::$class_arr]);
    }

    private function app_version_edit($data){
        $model = AppVersionModel::findOne($data['id']);
        if($model){
            $model->setScenario('edit');
        }else{
            $model = new AppVersionModel(['scenario'=>'add']);
        }
        if ($model->load($data,'') && $model->validate() && $model->save()) {
            $this->addRecord('编辑配置',$model->id);
            return json_encode(parent::ajaxReturn('operate_success'));
        }
        return json_encode(parent::ajaxReturn('edit_error'));
    }
}
